package com.sl.au.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ContractProductInfoView;
import com.sl.au.entity.ProductType;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.service.OptionService;
import com.sl.au.service.ProducTypeService;
import com.sl.au.service.SaleAreaService;
import com.sl.au.service.GetProductChartService;

@RestController
@RequestMapping(value = "/ui/productTotal")
public class ProductTotalViewController extends BaseController{
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	SaleAreaService saleAreaService;
	
	@Autowired
	GetProductChartService getProductChartService;
	
	@Autowired
	ProducTypeService producTypeService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("productTotal/productIndex");
		//
		System.out.println(request.getParameter("deptName"));
		String[] areaNames = null;
		String[] superiors = null;
		if(StringUtils.hasText(request.getParameter("areaName"))){
			areaNames = request.getParameter("areaName").toString().split(",");//片区
		}
		if(StringUtils.hasText(request.getParameter("deptName"))){
			superiors = request.getParameter("deptName").toString().split(",");//部门
		}
		List<SaleArea> orderAreas = saleAreaService.getRepository().getAllSaleArea();
		List<SaleArea> SaleOrderAreas = new ArrayList<SaleArea>();//销售
		List<SaleArea> CompleteOrderAreas = new ArrayList<SaleArea>();//成套
		List<SaleArea> ForeignOrderAreas = new ArrayList<SaleArea>();//外贸
		List<SaleArea> shOrderAreas = new ArrayList<SaleArea>();//售后服务处
		for(SaleArea orderArea : orderAreas){
			orderArea.setIsSelected(false);
			if(orderArea.getSuperior().equals("销售处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("销售处-分管领导")){
				}else{
					if(orderArea.getAreaName().equals("科长")){
					}else{
						SaleOrderAreas.add(orderArea);
					}
				}
			}
			if(orderArea.getSuperior().equals("成套处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("其他")){
				}else{
					if(orderArea.getAreaName().equals("成套处分管领导")){
					}else{
						CompleteOrderAreas.add(orderArea);
					}
				}
			}
			if(orderArea.getSuperior().equals("外贸处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("外贸处-分管领导")){
				}else{
					ForeignOrderAreas.add(orderArea);
				}
			}
			
			if(orderArea.getSuperior().equals("售后服务处")){
				if(superiors!=null&&superiors.length>0){
					if(areaNames!=null&&areaNames.length>0){
						for(int i=0;i<areaNames.length;i++){
							if(areaNames[i].equals(orderArea.getSuperiorArea()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<superiors.length;i++){
							if(superiors[i].equals(orderArea.getSuperior()))
								orderArea.setIsSelected(true);
						}
					}
				}
				if(orderArea.getAreaName().equals("售后服务处-分管领导")){
				}else{
					shOrderAreas.add(orderArea);
				}
			}
		}
		modelAndView.addObject("SaleOrderAreas", SaleOrderAreas);
		modelAndView.addObject("CompleteOrderAreas", CompleteOrderAreas);
		modelAndView.addObject("ForeignOrderAreas", ForeignOrderAreas);
		modelAndView.addObject("shOrderAreas", shOrderAreas);
		
		String[] bigtype = null;
		String[] smalltype = null;
		if(StringUtils.hasText(request.getParameter("largeType"))){
			bigtype = request.getParameter("largeType").toString().split(",");//大 类型
		}
		if(StringUtils.hasText(request.getParameter("smallType"))){
			smalltype = request.getParameter("smallType").toString().split(",");//小类型
		}
		List<ProductType> dboSmallType = producTypeService.getRepository().getAllType();
		List<ProductType> jlb = new ArrayList<ProductType>();//计量泵
		List<ProductType> wfb = new ArrayList<ProductType>();//往复泵
		List<ProductType> ctzz = new ArrayList<ProductType>();//成套装置
		List<ProductType> lxb = new ArrayList<ProductType>();//离心泵
		List<ProductType> rsxhb = new ArrayList<ProductType>();//热水循环泵
		List<ProductType> clb = new ArrayList<ProductType>();//齿轮泵
		List<ProductType> sjb = new ArrayList<ProductType>();//深井泵
		List<ProductType> gyclxt = new ArrayList<ProductType>();//高压水除鳞系统
		List<ProductType> wxb = new ArrayList<ProductType>();//维修泵
		List<ProductType> sb = new ArrayList<ProductType>();//三包
		List<ProductType> bf = new ArrayList<ProductType>();//补发
		List<ProductType> bj = new ArrayList<ProductType>();//备件
		List<ProductType> wfl = new ArrayList<ProductType>();//未分类
		for(ProductType orderArea : dboSmallType){
			orderArea.setIsSelected(false);
			if(orderArea.getcLargeType().equals("计量泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				jlb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("往复泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				wfb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("成套装置")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				ctzz.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("离心泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				lxb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("热水循环泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				rsxhb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("齿轮泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				clb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("深井泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				sjb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("高压水除鳞系统")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				gyclxt.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("维修泵")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				wxb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("三包")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				sb.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("补发")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				bf.add(orderArea);
			}
			
			if(orderArea.getcLargeType().equals("备件")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				bj.add(orderArea);
			}
			if(orderArea.getcLargeType().equals("未分类")){
				if(bigtype!=null&&bigtype.length>0){
					if(smalltype!=null&&smalltype.length>0){
						for(int i=0;i<smalltype.length;i++){
							if(smalltype[i].equals(orderArea.getcSmallType()))
								orderArea.setIsSelected(true);
						}
					}else{
						for(int i=0;i<bigtype.length;i++){
							if(bigtype[i].equals(orderArea.getcLargeType()))
								orderArea.setIsSelected(true);
						}
					}
				}
				wfl.add(orderArea);
			}
			
		}
		modelAndView.addObject("jlb", jlb);
		modelAndView.addObject("wfb", wfb);
		modelAndView.addObject("ctzz", ctzz);
		modelAndView.addObject("lxb", lxb);
		modelAndView.addObject("rsxhb", rsxhb);
		modelAndView.addObject("clb", clb);
		modelAndView.addObject("sjb", sjb);
		modelAndView.addObject("gyclxt", gyclxt);
		modelAndView.addObject("wxb", wxb);
		modelAndView.addObject("sb", sb);
		modelAndView.addObject("bf", bf);
		modelAndView.addObject("bj", bj);
		modelAndView.addObject("wfl", wfl);
		
		modelAndView.addObject("industryInfos", optionService.getRepository().findByGroup("行业"));//行业
		modelAndView.addObject("industryInfo1", request.getParameter("industryName")!=null ? request.getParameter("industryName") : "");//行业
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String s = df.format(new Date());
		Calendar a=Calendar.getInstance(); 
		int aa = a.get(Calendar.YEAR);
		modelAndView.addObject("startDate", request.getParameter("beginDate")!= null ? request.getParameter("beginDate") : a.get(Calendar.YEAR)+"-01-01");//开始时间
		modelAndView.addObject("endDate", request.getParameter("endDate")!=null ? request.getParameter("endDate") : df.format(new Date()));//结束时间
		//modelAndView.addObject("nameDate", request.getParameter("dateTimeType")!=null?request.getParameter("dateTimeType"):"");//时间类型
		
		//modelAndView.addObject("isSuccess", request.getParameter("isSuccess") != null ? request.getParameter("isSuccess") : "" );//部门柱状图是否成功
		/*if(request.getParameter("isSuccess") != null){
			modelAndView.addObject("isSuccess", request.getParameter("isSuccess").toString().equals("所有") ? "" : request.getParameter("isSuccess") );//状态分析
		}else{
			modelAndView.addObject("isSuccess", "" );//状态分析	
		}
		modelAndView.addObject("chanceCount", request.getParameter("chanceCount") != null ? request.getParameter("chanceCount") : "" );//信用评分
*/		return modelAndView;
	}
	
	//获取主界面列表中的数据
			@RequestMapping(value = "getProductTotalPage", method = RequestMethod.GET)
			public Map<String, Object> getChanceTotalPage(HttpServletRequest request) {
				Map<String, Object> map = new HashMap<String, Object>();
				Map<String, Object> qMap = new HashMap<String, Object>();
				Map<String, Object> condition = new HashMap<String, Object>();
				Map<String, Object>numMap=new HashMap<String, Object>();
				int start = Integer.parseInt(request.getParameter("start"));
				int length = Integer.parseInt(request.getParameter("length"));
				Account a = this.getMyAccount();
				String dataFilter = "";
				User user = a.getUser();
				this.getQueryParams(qMap,numMap);
				condition.put("qMap", qMap);
				condition.put("numMap", numMap);
				
				String userId = a.getUser().getId();
				String Superior = a.getUser().getSaleArea().getSuperior();//权限部门
				String areaName = a.getUser().getSaleArea().getAreaName();
				if (userId == null) {
					userId = "";
				}
				if (Superior == null) {
					Superior = "";
				}
				if (areaName == null) {
					areaName = "";
				}
				/*if (dataFilter == null) {
					dataFilter = "";
				}*/
				condition.put("userId", userId);
				condition.put("superior", Superior);
				condition.put("areaName", areaName);
				condition.put("dataFilter", dataFilter);
				List<ContractProductInfoView> contracts = getProductChartService.getProductTotal(start, length, condition, user, "createTime", "desc");
				List<ContractProductInfoView> contractsum = getProductChartService.getProductTotal(0, Integer.MAX_VALUE, condition, user, "createTime","desc");
				long count = contractsum.size();
				map.put("data", contracts);
				map.put("draw", request.getParameter("draw"));
				map.put("recordsFiltered", count);
				map.put("recordsTotal", count);
				return map;
			}
			/**
			 * hql
			 * @param qMap
			 * @param numMap
			 */
				private void getQueryParams(Map<String, Object> qMap,Map<String, Object>numMap){
					String names[]={"orderArea","industryInfo","nUnitPrice","detailType"};//部门-片区，行业，单价范围
					String dates[] = {"createTime"};
					for(String name:names){
						System.out.println(request.getParameter(name));
						String aa =  request.getParameter(name);
						if(StringUtils.hasText(request.getParameter(name))){
							String[] a = request.getParameter(name).toString().split(",");
							String b = "";
							for(int i=0;i<a.length;i++){
								b += "'" + a[i] + "',";
							}
							b = b.substring(0, b.length()-1);
							numMap.put(name, b);
						}
					}
					if(StringUtils.hasText(request.getParameter("supplierCode"))){//合同号
						numMap.put("supplierCode", request.getParameter("supplierCode").toString());
					}
					if(StringUtils.hasText(request.getParameter("orderUnit"))){//订货单位
						System.out.println(request.getParameter("orderUnit"));
						numMap.put("orderUnit", request.getParameter("orderUnit").toString());
					}
					for(String date:dates){
						if(null!=this.getDateQueryItem(date)){
							qMap.put(date,this.getDateQueryItem(date));
						}
					}
					
				}

}
